package com.iss.card.common;

import android.app.ActivityManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;

import com.iss.card.R;

public class UtilityHelper {
	private static UtilityHelper sInstance;
	private Context mContext;

	public synchronized static UtilityHelper instance() {
		if (sInstance == null) {
			sInstance = new UtilityHelper();
		}
		return sInstance;
	}

	public void setContext(Context c) {
		mContext = c;
	}

	public int getContentHeight() {
		final Resources r = mContext.getResources();
		final float scale = r.getDisplayMetrics().density;
		return (int) (ApplicationManager.SCREEN_HEIGHT - ((r
				.getDimension(R.dimen.DIM_PAGE_FOOTER_HEIGHT) + r
				.getDimension(R.dimen.DIM_PAGE_HEADER_HEIGHT))
				/ scale + 0.5f));
	}

	public int pxTodp(float pxVal) {
		final float scale = mContext.getResources().getDisplayMetrics().density;
		return (int) (pxVal / scale + 0.5f);
	}

	public int dpTopx(int dpVal) {
		final float scale = mContext.getResources().getDisplayMetrics().density;
		return (int) (dpVal * scale + 0.5f);
	}

	public SharedPreferences getSharedPreferences(String sp) {
		return mContext.getSharedPreferences(sp, mContext.MODE_PRIVATE);
	}

	public String getMemoryInfo() {
		final ActivityManager activityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
		activityManager.getMemoryInfo(outInfo);
		StringBuffer buffer = new StringBuffer();
		buffer.append("\n剩余内存:---->").append(outInfo.availMem >> 10)
		.append("k");
		buffer.append("\n剩余内存:---->").append(outInfo.availMem >> 20)
		.append("M");
		buffer.append("\n是否处于低内存状态:----->").append(outInfo.lowMemory);
		return buffer.toString();
	}
}
